package com.qst.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qst.pojo.*;
import com.qst.service.DepartmentService;
import com.qst.service.MeetingRoomService;
import com.qst.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

@Controller
public class MeetingController {

    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private MeetingRoomService meetingRoomService;
    @Autowired
    private SearchService service;

    @RequestMapping("/searchmeeting")
    public String searchmeetings(Model model, String meetingname, String roomname, String reservationistname, Date reservationtime, Date starttime, Date endtime,@RequestParam(defaultValue = "1") Integer page) {

        SearchMeeting searchMeeting = new SearchMeeting();
        searchMeeting.setName(reservationistname);
        Meeting meeting = new Meeting();
        MeetingRoom room = new MeetingRoom();
        room.setRoomName(roomname);
        meeting.setMeetingRoom(room);
        meeting.setMeetingname(meetingname);
        meeting.setReservationtime(reservationtime);
        meeting.setStarttime(starttime);
        meeting.setEndtime(endtime);
        searchMeeting.setMeeting(meeting);

        //保存前端传来的额信息
        model.addAttribute("searchMeeting", searchMeeting);

        List<SearchMeeting> meetings = service.findMeeting(searchMeeting, page);
        //保存查询结果
        model.addAttribute("searchMeetings", meetings);
        PageInfo<SearchMeeting> pageInfo = new PageInfo<>(meetings);
        //总条数
        model.addAttribute("total", pageInfo.getTotal());
        //总页数
        model.addAttribute("page", pageInfo.getPageNum());
        //当前页码
        model.addAttribute("pagenum", pageInfo.getPages());

        return "searchmeetings";
    }

    @RequestMapping("/searchemployee")
    public String searchemployee(Employee employee, HttpServletRequest request) {
        List<Employee> employees = service.findEmployee(employee);
        request.setAttribute("employees", employees);
        return "/searchemployees";
    }

    @RequestMapping("/bookmeeting")
    public String bookMeeting(Model model) {
        List<Department> departmentList = departmentService.selectDep();
        model.addAttribute("departmentList", departmentList);
        List<MeetingRoom> meetingRoomList = meetingRoomService.getAllMeetingRoom();
        model.addAttribute("departmentList", departmentList);
        model.addAttribute("meetingRoomList", meetingRoomList);
        return "bookmeeting";
    }

    @RequestMapping("/findallmeetings")
    public String findallmeetings(Model model, HttpSession session) {
        Employee curUser = (Employee) session.getAttribute("curUser");
        List<Meeting> meetingList = service.findMeetingByRid(curUser.getEmployeeId());
        model.addAttribute("allmeetings", meetingList);
        return "mybookings";
    }

    @RequestMapping("/getemployee")
    @ResponseBody
    public Object getEmployee(int dpId) {
        return departmentService.selectEmployeeByDp(dpId);
    }

    @RequestMapping("/judgeTime")
    @ResponseBody
    public Object judgeTime(int peopleNum, String date, String startTime, String endTime) throws ParseException {
        List<MeetingRoom> meetingRoomList = meetingRoomService.getLeisureMeetingRoom(peopleNum, date, startTime, endTime);
        meetingRoomList.forEach(meetingRoom -> System.out.println(meetingRoom));
        return meetingRoomList;
    }

    @PostMapping("/saveMeeting")
    public String saveMeeting(String meetingname, String numberofparticipants, String time, String starttime, String endtime, String roomid, String description, String mps, HttpSession session) throws ParseException {
        int reservationistid = ((Employee) session.getAttribute("curUser")).getEmployeeId();
        meetingRoomService.addMeeting(meetingname, numberofparticipants, reservationistid, time, starttime, endtime, roomid, description, mps);

        return "forward:/meetingroom";
    }
}
